<template>
  <div class="goods_list">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        :offset="50"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="加载更多"
        @load="onLoad"
      >
        <van-cell @click="goDetail(item.id)" v-for="item in goods_list" :key="item.id">
          <van-image fit="cover" :src="item.img_url"></van-image>
          <p>{{ item.title }}</p>
          <div class="bottom_box">
            <div>
              <span>￥{{ item.sell_price }}</span>
              <s>￥{{ item.market_price }}</s>
            </div>
            <div>
              <span>热卖中</span>
              <span>剩{{ item.stock_quantity }}件</span>
            </div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goods_list: [],
      loading: false,
      finished: false,
      number: 0,
      isLoading: false,
      error: false
    }
  },

  methods: {
    onLoad() {
      this.number++
      this.getList()
    },

    async getList() {
      var { data: res } = await this.$http.get(
        '/api/getgoods?pageindex=' + this.number
      )
      if (res.status !== 0) {
        this.error = true
        return
      }
      if (res.message.length === 0) {
        this.loading = false
        this.finished = true
        return
      }
      this.goods_list = this.goods_list.concat(res.message)
      this.loading = false
      console.log(res)
    },

    onRefresh() {
      this.number = 1
      this.goods_list = []
      this.getList().then(() => {
        this.isLoading = false
        this.finished = false
      })
    },

    goDetail(id) {
      this.$router.push('/goods/detail/' + id)
    }
  },

  created() {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.goods_list {
  padding: 30px 0;
}
.van-list {
  /deep/.van-list__finished-text {
    width: 100%;
  }
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 5px 10px;
  .van-cell {
    width: 48%;
    border: 1px solid #ccc;
    margin: 10px 0px;
    padding: 0px;
    .van-cell__value--alone {
      text-align: center;
      .van-image {
        padding: 10px;
        min-height: 180px;
      }
    }
    .van-cell__value {
      > p {
        font-size: 14px;
        color: #000;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        padding: 5px 10px;
        height: 49px;
      }
    }
    .bottom_box {
      width: 100%;
      background: #eee;
      padding: 5px 0px;
      div:nth-child(1) {
        text-align: left;
        padding: 0px 10px;
        span:nth-child(1) {
          font-size: 16px;
          color: red;
          margin-right: 15px;
        }
      }
      div:nth-child(2) {
        display: flex;
        justify-content: space-between;
        padding: 0 10px;
      }
    }
  }
}
</style>
